package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.bean.AddressCheckOutInfo;
import com.itheima.redbaby.bean.CheckAddUp;
import com.itheima.redbaby.bean.DeliveryInfo;
import com.itheima.redbaby.bean.PaymentInfo;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.dao.CartDao;
import com.itheima.redbaby.dao.PaymentInfoDao;
import com.itheima.redbaby.dao.domain.PaymentInfoPo;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.CheckOutEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.net.HttpUtil;

public class CheckOutEngineImpl extends BaseEngine implements CheckOutEngine {
	@Override
	public AddressCheckOutInfo getServerAddressCheckoutInfo() {	
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		Object json = getJson(BaseEngine.TYPE_POST, GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params);
		try{
			JSONObject jsonObject=new JSONObject();
			jsonObject=JSON.parseObject(json.toString());
			
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String address_info = jsonObject.getString("address_info");
				AddressCheckOutInfo addressInfo = JSONObject.parseObject(address_info,AddressCheckOutInfo.class);
				return addressInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}//相当于服务器中map集合
		return null;
	}
	@Override
	public PaymentInfo getServerPaymentInfo() {
		//PaymentInfoDao paymentInfoDao=DaoAndEngineFactory.getImpl(PaymentInfoDao.class);
		//paymentInfoDao.getPaymentInfo();
		//1.读取response字段
		//2.判断是否为 checkout
		HttpUtil httpUtil=new HttpUtil();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		String userid=null;
		String json = httpUtil.sendPost(GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params, userid);
		try{
			JSONObject jsonObject=new JSONObject();
			jsonObject=JSON.parseObject(json);
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String payment_info = jsonObject.getString("payment_info");
				PaymentInfo paymentInfo =JSONObject.parseObject(payment_info, PaymentInfo.class);
				//开启子线程 持久化数据到本地
				if(paymentInfo!=null){
					PaymentInfoPo paymentInfoPo=new PaymentInfoPo();
					paymentInfoPo.setOrderid(paymentInfo.getOrderid());
					paymentInfoPo.setType(paymentInfo.getType());
					//插入到数据库
//					paymentInfoDao.insert(paymentInfoPo);
				}
				return paymentInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}//相当于服务器中map集合
		return null;
	}
	@Override
	public DeliveryInfo getServerDeliveryInfo() {
		//1.读取response字段
		//2.判断是否为 checkout
		HttpUtil httpUtil=new HttpUtil();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		String userid=null;
		String json = httpUtil.sendPost(GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params, userid);
		try{
			JSONObject jsonObject=new JSONObject();
			jsonObject=JSON.parseObject(json);
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String delivery_info = jsonObject.getString("delivery_info");
				DeliveryInfo deliveryInfo = JSONObject.parseObject(delivery_info, DeliveryInfo.class);
				return deliveryInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}//相当于服务器中map集合
		return null;
	}
	@Override
	public List<Product> getServerProductList() {
		//1.读取response字段
		//2.判断是否为 checkout
		HttpUtil httpUtil=new HttpUtil();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		String userid=null;
		String json = httpUtil.sendPost(GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params, userid);
		try{
			JSONObject jsonObject=new JSONObject();
			
			jsonObject=JSON.parseObject(json);
//			json
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String productlist = jsonObject.getString("productlist");
				List<Product> products = JSONObject.parseArray(productlist,Product.class);
				System.out.println(products);
				return products;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}//相当于服务器中map集合
		return null;
	}
	/**
	 * 优惠信息的获取
	 */
	@Override
	public String[] getServerCheckout_prom() {
		//1.读取response字段
		//2.判断是否为 checkout
		HttpUtil httpUtil=new HttpUtil();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		String userid=null;
		String json = httpUtil.sendPost(GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params, userid);
		try{
			JSONObject jsonObject=new JSONObject();
			jsonObject=JSON.parseObject(json);
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String checkout_prom = jsonObject.getString("checkout_prom");
				String[] split = checkout_prom.split(",");
				return split;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}//相当于服务器中map集合
		return null;
	}
	@Override
	public CheckAddUp getServerCheckout_addup() {
		HttpUtil httpUtil=new HttpUtil();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("response", "checkout");
		String userid=null;
		String json = httpUtil.sendPost(GloableParams.LZLURLCOMMON+GloableParams.LZLCONTENT, params, userid);
		try{
			JSONObject jsonObject=new JSONObject();
			jsonObject=JSON.parseObject(json);
			String res = jsonObject.getString("response");//得到解析中的response信息
			if(res.equals("checkout")){
				String checkout_addup = jsonObject.getString("checkout_addup");
				CheckAddUp checkoutAddup = JSONObject.parseObject(checkout_addup, CheckAddUp.class);
				return checkoutAddup;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
